package com.okilo.online.app.pet

import com.okilo.online.app.common.IOkiloComView
import com.okilo.online.app.common.OkiloComPet
import com.okilo.online.app.configs.OkiloConfigs
import com.okilo.online.app.configs.OkiloConfigs.addParams
import com.okilo.online.app.request.IHttpCallback
import com.okilo.online.app.pet.view.IOkiloLoan
import org.json.JSONObject

/**
 * 额度、申贷、贷款信息
 */
class OkiloLoanPet(private val iView: IOkiloComView?): OkiloComPet(iView) {

    /**
     * 获取首页显示额度
     */
    fun gOkiloHomeLimitRequest(callback: IOkiloLoan.IOkiloHomeLimit){
        val params = JSONObject()
            .addParams()
            .apply {
                put("path", OkiloConfigs.gOkiloRequestPath("LIMIT"))
            }
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                val limitArray = obj?.optJSONArray("limit")
                limitArray?.let {
                    if (it.length() > 0){
                        val limitObj = it.optJSONObject(0)
                        callback.sOkiloHomeLimitCallback(limitObj)
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                gOkiloHomeLimitIncreasedRequest(callback)
            }
        })
    }

    /**
     * 加载产品额度是否增加
     */
    private fun gOkiloHomeLimitIncreasedRequest(callback: IOkiloLoan.IOkiloHomeLimit){
        val params = JSONObject()
            .addParams()
            .apply {
                put("path", OkiloConfigs.gOkiloRequestPath("LIMIT_RAISE"))
            }
        //todo 测试提额dialog
//        params.put("selectAmount","2000")
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                obj?.let {
                    if (it.optString("openFlag") == "YES"){
                        callback.sOkiloHomeLimitIncreasedCallback(it)
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.sOkiloHomeRequestFinish()
            }
        })
    }

    /**
     * 获取贷款额度信息（申贷）
     */
    fun gOkiloApplyDetailsRequest(callback: IOkiloLoan.IOkiloApply){
        val params = JSONObject().addParams()
        params.put("path", OkiloConfigs.gOkiloRequestPath("APPLY_DETAILS"))
        params.put("amount",callback.gOkiloApplyAmount())
        params.put("oldFlag","YES")
        val applyDays = callback.gOkiloApplyTerm()
        if (applyDays.isNotEmpty()){
            params.put("days",applyDays)
        }
        val httpUrl = OkiloConfigs.gOkiloApplyUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //是否允许修改申请金额，不允许修改则不显示Seekbar
                    val chooseAmountFlag = obj.optBoolean("chooseAmountFlag",false)
                    when(chooseAmountFlag){
                        true -> {//允许选择贷款额度
                            callback.sOkiloApplySelectableAmountsViews(obj)
                        }
                        else -> {//不允许选择贷款额度
                            callback.sOkiloApplyFixedAmountsViews()
                        }
                    }
                    callback.sOkiloApplyTermList(obj)
                    callback.sOkiloApplyDetailsList(obj)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView?.hideOkiloLoading()
            }
        })
    }

    /**
     * 创建贷款申请订单
     */
    fun subOkiloApplyRequest(callback: IOkiloLoan.IOkiloApply){
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("APPLY_CREATE_ORDER"))
            put("amount",callback.gOkiloApplyAmount())
            put("days",callback.gOkiloApplyTerm())
        }
        val httpUrl = OkiloConfigs.gOkiloApplyUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.sOkiloApplySuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取贷款信息
     */
    fun gOkiloApplyOrderDetailsRequest(callback: IOkiloLoan.IOkiloOrderInfo){
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("LOAN_DETAILS"))
        }
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    callback.sOkiloApplyOrderInfo(obj)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * 获取还款明细
     */
    fun gOkiloRepayInfoRequest(callback: IOkiloLoan.IOkiloRepay){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("REPAY_INFO"))
            put("oldFlag","YES")
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                obj?.let {
                    callback.sOkiloLoadRepayInfo(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * 获取还款链接
     *
     * 订单还款
     * 不减免 normal
     * 积分减免 score
     * 优惠券减免 coupon
     */
    fun gOkiloPayLinkRequest(callback: IOkiloLoan.IOkiloPay){
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        val params = JSONObject().addParams().apply {
            put("mobile", OkiloConfigs.gOkiloLoginMobile())
            put("path", OkiloConfigs.gOkiloRequestPath("REPAY_LINK"))
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    callback.sOkiloPayDataCallback(obj)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }

    /**
     * PIN 流程 查询支付状态
     */
    fun gOkiloPinPayStatusRequest(thirdOrderId: String, isStartCountdown: Boolean = true, callback: IOkiloLoan.IOkiloPinPayStatus){
        val params = JSONObject().addParams().apply {
            put("path", OkiloConfigs.gOkiloRequestPath("PIN_PAY_STATUS"))
            put("thirdOrderId",thirdOrderId)//三方订单id
        }
        val httpUrl = OkiloConfigs.gOkiloRequestUrl()
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.hideOkiloLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    obj.put("isStartCountdown",isStartCountdown)
                    callback.sOkiloPinPayStatusData(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                callback.hideOkiloLoading()
            }
        })
    }
}